package com.edgclub.edg.fragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.edgclub.edg.R;
import com.edgclub.edg.activity.GzWebViewActivity;
import com.edgclub.edg.adapter.EDGTeamAdapter;
import com.edgclub.edg.adapter.EDGTeamPopAdapter;
import com.edgclub.edg.adapter.adapteruitls.MyItemClickListener;
import com.edgclub.edg.api.ApiConfig;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.TopicApi;
import com.edgclub.edg.api.param.PostsParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.model.User;
import com.edgclub.edg.model.result.EDGTeamData;
import com.edgclub.edg.model.result.EDGTeamType;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.edgclub.edg.utils.util.DensityUtils;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.JsonUtil;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * edg队员列表
 */
public class EDGTeamFragment extends GzBaseFragment implements View.OnClickListener{
	private Activity mActivity;

	private boolean isLoading = false; // 是否正在加载
	private boolean isFirstLoad = true;

	private int currPage = 1;
	private int pageSize = 20;
	private TextView tv_title;
	private ImageView imgv_down,imgv_icon;
	private XRecyclerView recyclerView;
	private UserPrefUtils userPrefUtils;
	private EDGTeamAdapter edgTeamAdapter;
	private ArrayList<EDGTeamData.Team> list;
	private TopicApi topicApi;
	private int spacingInPixels;

	private PopupWindow popWindow = null; // 菜单
	private EDGTeamPopAdapter popAdapter;
	private ArrayList<EDGTeamType> typeList;
	private TextView tv_tmp;

	private HashMap<EDGTeamType,ArrayList<EDGTeamData.Team>> hashMap;

	private GlideUtils glideUtils;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mActivity = getActivity();
		View rootView= inflater.inflate(R.layout.fragment_edg_team, container, false);
		return rootView;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		initView(view);
		initData();

	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		if(isVisibleToUser && isFirstLoad){
			isFirstLoad = false;
			currPage=1;
			getteamList(currPage);
		}else{

		}
	}

	private void initView(View view){
		initLoadView(view);
		spacingInPixels = DensityUtils.dip2px(mActivity,5);
		imgv_down = (ImageView) view.findViewById(R.id.imgv_down);
		imgv_icon = (ImageView) view.findViewById(R.id.imgv_icon);
		imgv_down.setOnClickListener(this);
		recyclerView = (XRecyclerView) view.findViewById(R.id.recyclerview);
		tv_title = (TextView) view.findViewById(R.id.tv_title);
		GridLayoutManager layoutManager = new GridLayoutManager(mActivity,2);
		recyclerView.setLayoutManager(layoutManager);
		recyclerView.setPullRefreshEnabled(false);
		recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
			@Override
			public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
				int pos = parent.getChildAdapterPosition(view);
				if (pos%2!=0) {
					outRect.left = 0;
				} else {
					outRect.left = spacingInPixels;
				}
				outRect.top = spacingInPixels*2;
			}
		});
	}

	private void initData(){
		topicApi = new TopicApi(getActivity());
		userPrefUtils = new UserPrefUtils(getActivity());
		glideUtils = new GlideUtils();
		list = new ArrayList<>();
		typeList = new ArrayList<>();
		hashMap = new HashMap<>();
		edgTeamAdapter = new EDGTeamAdapter(getActivity());
		edgTeamAdapter.setList(list);
		recyclerView.setAdapter(edgTeamAdapter);
		edgTeamAdapter.setOnItemClickListener(new MyItemClickListener() {
			@Override
			public void onItemClick(View view, int postion) {
				Intent intent=new Intent(getActivity(), GzWebViewActivity.class);
				intent.putExtra("url", ApiConfig.USER_PLAYER_DETAIL+list.get(postion).getUserId());
				intent.putExtra("actbarTitle",list.get(postion).getName());
				intent.putExtra("isHasTitle",false);
				startActivity(intent);
			}
		});
	}

	private void getteamList(int page) {
		if (isLoading) {
			return;
		}
		User user = userPrefUtils.getUser();
		PostsParam postsParam = new PostsParam();
		postsParam.setPage(page + "");
		postsParam.setPageSize(pageSize + "");
		postsParam.setUserId(user.getId() + "");
		topicApi.getEdgTeam(postsParam, new getListListener());//edg列表
	}


	class getListListener implements VolleyListener {

		@Override
		public void onStart(String tag) {
			isLoading = true;
		}

		@Override
		public void onSuccess(String response, String tag) {

			if (JSONHelper.isSuccess(response)) {
				EDGTeamData data = JsonUtil.getMode(response, EDGTeamData.class);
				ArrayList<EDGTeamData.EdgTeam> edgTeams = data.getData();
				for(int i=0;i<edgTeams.size();i++){
					EDGTeamType teamType = new EDGTeamType(edgTeams.get(i).getCategory(),edgTeams.get(i).getDefaultLogo(),
							edgTeams.get(i).getCheckLogo(),edgTeams.get(i).getUnCheckLogo());
					hashMap.put(teamType,edgTeams.get(i).getData());
					typeList.add(teamType);
					tv_title.setText(edgTeams.get(0).getCategory()+"");
					glideUtils.Glidde_loadImage(mActivity,edgTeams.get(0).getDefaultLogo(),imgv_icon);
					list.clear();
					list.addAll(edgTeams.get(0).getData());
				}
				edgTeamAdapter.setList(list);
				edgTeamAdapter.notifyDataSetChanged();

			} else {

			}

			isLoading = false;
			recyclerView.refreshComplete();
			recyclerView.loadMoreComplete();
			setLoadViewGONE();
		}

		@Override
		public void onFaile(VolleyError volleyError, String tag) {
			isLoading = false;
			recyclerView.refreshComplete();
			recyclerView.loadMoreComplete();
			setNoNetWork(new ReLoadClickListener() {
				@Override
				public void reload() {
					getteamList(currPage);
				}
			});
			ResultCode.toastVolleyError(getActivity(), volleyError);
		}
	}


	@Override
	public void onClick(View view) {
		switch (view.getId()){
			case R.id.imgv_down:
				showpopWindow(view,typeList);
				break;
		}
	}



	public void showpopWindow(View v, final ArrayList<EDGTeamType> typeList) {
		if (popWindow == null) {
			View view = (LinearLayout) LayoutInflater.from(this.getActivity())
					.inflate(R.layout.pop_team_menu, null);
			tv_tmp = (TextView) view.findViewById(R.id.tv_tmp);
			popWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
//			popWindow.setWidth(DensityUtils.getScreenW(this.getActivity())/2);
			popWindow.setOutsideTouchable(true);
			popWindow.setFocusable(true);
			popWindow.setBackgroundDrawable(new ColorDrawable());

			popWindow.setContentView(view);
			ListView listView = (ListView) view.findViewById(R.id.pop_recycleView);
			popAdapter = new EDGTeamPopAdapter(mActivity);
			popAdapter.setList(typeList);
			listView.setAdapter(popAdapter);
			listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
					popWindow.dismiss();
					popAdapter.selectIndex(i);
					popAdapter.notifyDataSetChanged();
					EDGTeamType type = typeList.get(i);
					ArrayList<EDGTeamData.Team> teams = hashMap.get(type);
					list.clear();
					list.addAll(teams);
					edgTeamAdapter.notifyDataSetChanged();
					tv_title.setText(type.getCategory()+"");
					glideUtils.Glidde_loadImage(mActivity,type.getDefaultLogo(),imgv_icon);
				}
			});
			popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
				@Override
				public void onDismiss() {
					imgv_down.setImageResource(R.drawable.team_down);
				}
			});
		}

		popAdapter.setList(typeList);
		popAdapter.notifyDataSetChanged();
		popWindow.showAsDropDown(v);
		imgv_down.setImageResource(R.drawable.team_up);
	}

}
